
<!-- 报警统计 -->
<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item :label="v.name" :prop="v.prop" :style="{ width: v.width }" v-for="v in searchItems">
                <el-input v-if="v.type == 'input'" v-model="queryParams[v.prop]" :placeholder="'请输入' + v.name" clearable
                    @keyup.enter="handleQuery" />
                <el-tree-select v-else-if="v.type == 'treeSelect'" v-model="queryParams[v.prop]"
                    :placeholder="'请输入' + v.name" clearable :data="sel_option[v.prop]" :expand-on-click-node="false"
                    :render-after-expand="false" @change="handleQuery" check-strictly :props="{ label: 'label' }"
                    node-key="id" filterable />
                <el-date-picker v-else-if="v.type == 'dateTime'" v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss"
                    type="datetimerange" :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                    new Date(2000, 2, 1, 23, 59, 59),]" range-separator="-" start-placeholder="开始时间"
                    end-placeholder="结束时间"></el-date-picker>
                <el-date-picker v-else-if="v.type == 'date'" v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss"
                    :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                    new Date(2000, 2, 1, 23, 59, 59),]" type="daterange" range-separator="-" start-placeholder="开始日期"
                    end-placeholder="结束日期"></el-date-picker>
                <el-select v-else-if="v.type == 'select'" v-model="queryParams[v.prop]" clearable filterable
                    :placeholder="'请输入' + v.name" @change="handleQuery">
                    <el-option v-for="item in sel_option[v.prop]" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['statisticalReportForm:alarmStatistics:export']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table highlight-current-row v-loading="loading" :data="tableList">
            <template v-for="v in tableItems">
                <el-table-column :label="v.name" align="left" :prop="v.prop" v-if="v.type == 'time'" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row[v.prop], '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="v.name" align="left" :prop="v.prop" v-else :width="v.width" />
            </template>
            <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)"
                        v-hasPermi="['statisticalReportForm:alarmStatistics:detail']">明细</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>
<script setup lang="ts" name="AlarmStatistics">
import { useRouter } from 'vue-router';
import useAppStore from "../../store/modules/app";
const appStore = useAppStore()
const router = useRouter();
import dayjs from "dayjs"
import { reactive, toRefs, ref, getCurrentInstance } from "vue"
import { treeDept } from "../../api/system/dept";
import request from '../../utils/request'
const loading = ref(false);
const tableList = ref([]);
const { proxy } = getCurrentInstance();
//@ts-ignore
const parseTime = proxy.parseTime;
const tableItems = ref([
    { name: '设备名称', prop: "numberPlate" },
    { name: '设备号', prop: "deviceId" },
    { name: '所属公司', prop: "companyName", width: 320 },
    { name: '报警类型', prop: "name", width: 200 },
    { name: '报警等级', prop: "levelName" },
    { name: '报警时长(min)', prop: "minutes" },
    { name: '报警次数', prop: "count" },
    { name: '统计日期', prop: "time", type: 'time' },
    { name: '报警来源', prop: "sourceName" },
]);
const searchItems = ref([
    { name: '设备名称', prop: "numberPlate", type: 'input' },
    { name: '所属公司', prop: "companyId", type: 'treeSelect' },
    { name: '统计时间', prop: "time", type: "date", width: '' },
    { name: '报警类型', prop: "type", type: "select" },
    { name: '报警来源', prop: "source", type: "select" },
    { name: '报警等级', prop: "level", type: "select" },
]);
//@ts-ignore
const { alarmLevel, source } = proxy.useDict("alarmLevel", "source");
const sel_option: any = ref({ level: alarmLevel, source })
import { getAlarmTypeAll } from "../../api/alarmProcess/alarmProcess";
getAlarmTypeAll().then(resp => {
    sel_option.value.type = resp.data.map(p => {
        return { label: p.name, value: p.type, }
    })
})
const total = ref(0);
const showSearch = ref(true);
const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 15,
        companyId: "",
        numberPlate: "",
        type: "",
        level: "",
        source: "",
    },
});
const dateRange = ref([
    dayjs(new Date()).add(-1, "days").format("YYYY-MM-DD 00:00:00"),
    dayjs(new Date()).format("YYYY-MM-DD 23:59:59")
]);

treeDept().then(res => {
    sel_option.value.companyId = res.data
})
const { queryParams } = toRefs(data);

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    //@ts-ignore
    proxy.resetForm("queryRef");
    dateRange.value = [
        dayjs(new Date()).add(-1, "days").format("YYYY-MM-DD 00:00:00"),
        dayjs(new Date()).format("YYYY-MM-DD 23:59:59")
    ];
    handleQuery();
}
/** 查询列表 */
function getList() {
    loading.value = true;
    request({
        url: '/comlbs/reportStatistics/deviceAlarm/list',
        method: 'post',
        //@ts-ignore
        params: proxy.addDateRange2(queryParams.value, dateRange.value)
    }).then(({ data }: any) => {
        tableList.value = data.list;
        total.value = data.total;
        loading.value = false;
    }).catch(() => {
        loading.value = false;
    })
}

/** 详情按钮操作 */
function handleDetail(obj: any) {
    let time = obj.time;
    appStore.setPageQuery({
        dateRange: [time + ' 00:00:00', time + ' 23:59:59'],
        numberPlate: obj.numberPlate,
        companyId: obj.companyId,
        type: parseInt(obj.type),
        source: obj.source + '',
        level: obj.level + '',
    });
    router.push({ path: '/statisticalReports/alarmDetail' })
}

/** 导出按钮操作 */
function handleExport() {
    //@ts-ignore
    proxy.download('comlbs/reportStatistics/deviceAlarm/listExport',
        //@ts-ignore
        proxy.addDateRange2(queryParams.value, dateRange.value)
        , `报警统计_${dayjs(new Date()).format("YYYY-MM-DD_HH:mm:ss")}.xlsx`)
}
getList();
</script>
<style lang='less' scoped></style>